package Easy反转链表;

import org.testng.annotations.Test;

import java.util.ArrayList;
import java.util.List;

public class Solution {
    public ListNode reverseList(ListNode head) {
        ListNode rail = head;
        List<Integer> list = new ArrayList<>();
        while(rail!=null){
            list.add(rail.val);
            rail = rail.next;
        }
        reverse(head,list, list.size()-1);
        return head;
    }
    public void reverse(ListNode head, List<Integer> list, int i) {
        if (head!=null){
            head.val = list.get(i);
            reverse(head.next,list,i-1);
        }
    }
    @Test
    public void test(){
        ListNode l1 = new ListNode(1,new ListNode(2,new ListNode(3,new ListNode(4,
                new ListNode(5,null)))));
        l1 = reverseList(l1);
        while (l1!=null){
            System.out.println(l1.val);
            l1 = l1.next;

        }
    }
}
